package com.waimai1.waimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waimai1.waimai.common.R;
import com.waimai1.waimai.entity.*;
import com.waimai1.waimai.exception.EnumException;
import com.waimai1.waimai.exception.NotErrorException;
import com.waimai1.waimai.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/zhanshi")
@Slf4j
public class ZhanshiController {
    @Autowired
    private ZsGoodService zsGoodService;
    @Autowired
    private ZsSortbannerService zsSortbannerService;
    @Autowired
    private ZsSortService zsSortService;
    @Autowired
    private ZsBackService zsBackService;
    @Autowired
    private ZsPingbaoService zsPingbaoService;
    @GetMapping("/getBack")
    public R<ZsBack> getBack() {
        ZsBack res=zsBackService.getById(1);
        return R.success(res);
    }
    @GetMapping("/getGood/{pageIndex}")
    public R<Map<String, Object>> getGood(@PathVariable int pageIndex) {
        Page<ZsGood> page = new Page<>( pageIndex, 10);
        IPage<ZsGood> goodPage = zsGoodService.page(page);
        List<ZsGood> goodList = goodPage.getRecords();
        Long total = goodPage.getTotal();
        Map<String, Object> res = new HashMap<>();
        res.put("goodList", goodList);
        res.put("total", total);
        return R.success(res);
    }

    @GetMapping("/getSort/{pageIndex}")
    public R<Map<String, Object>> getSort(@PathVariable int pageIndex) {
        Page<ZsSort> page = new Page<>( pageIndex, 100);
        IPage<ZsSort> sortPage = zsSortService.page(page);
        List<ZsSort> sortList = sortPage.getRecords();
        Long total = sortPage.getTotal();
        Map<String, Object> res = new HashMap<>();
        res.put("sortList", sortList);
        res.put("total", total);
        return R.success(res);
    }
    @PostMapping("/getGoodByTab")
    public R<List<ZsGood>> getSort(@RequestBody Map<String,String> map) {
        LambdaQueryWrapper<ZsGood> goodLambdaQueryWrapper=new LambdaQueryWrapper<>();
        goodLambdaQueryWrapper.like(ZsGood::getSort,map.get("sort"));
        List<ZsGood> res=zsGoodService.list(goodLambdaQueryWrapper);
        return R.success(res);
    }
    @GetMapping("/getSortBanner")
    public R<ZsSortbanner> getSortBanner(){
        return R.success(zsSortbannerService.getById(1));
    }

    @GetMapping("/getPingbao/{pageIndex}")
    public R<Map<String, Object>> getPingbao(@PathVariable int pageIndex) {
        Page<ZsPingbao> page = new Page<>( pageIndex, 10);
        IPage<ZsPingbao> pingbaoPage = zsPingbaoService.page(page);
        List<ZsPingbao> pingbaoList = pingbaoPage.getRecords();
        Long total = pingbaoPage.getTotal();
        Map<String, Object> res = new HashMap<>();
        res.put("pingbaoList", pingbaoList);
        res.put("total", total);
        return R.success(res);
    }
    @RequestMapping(value={"/editGood"},method= RequestMethod.POST)
    public R<String> editGood(@RequestBody ZsGood zsGood){
        boolean res=zsGoodService.saveOrUpdate(zsGood);
        if(!res){
            throw new NotErrorException(EnumException.NO_UPDATE);
        }
        return R.success("ok");
    }
    @RequestMapping(value={"/saveSortImg"},method= RequestMethod.POST)
    public R<String> saveSortImg(@RequestBody ZsSortbanner zsSortbanner){
        zsSortbanner.setId(1);
        boolean res=zsSortbannerService.saveOrUpdate(zsSortbanner);
        if(!res){
            throw new NotErrorException(EnumException.NO_UPDATE);
        }
        return R.success("ok");
    }
    @PostMapping("/editSort")
    public R<String> editSort(@RequestBody ZsSort zsSort){
        boolean res=zsSortService.saveOrUpdate(zsSort);
        if(!res){
            throw new NotErrorException(EnumException.NO_UPDATE);
        }
        return R.success("ok");
    };
    @PostMapping("/editPingbao")
    public R<String> editPingbao(@RequestBody ZsPingbao zsPingbao){
        boolean res=zsPingbaoService.saveOrUpdate(zsPingbao);
        if(!res){
            throw new NotErrorException(EnumException.NO_UPDATE);
        }
        return R.success("ok");
    };
    @PostMapping("/delSort")
    public R<String> delSort(@RequestBody ZsSort zsSort){
        boolean res=zsSortService.removeById(zsSort);
        if(!res){
            throw new NotErrorException(EnumException.NO_DELETE);
        }
        return R.success("ok");
    };
    @PostMapping("/delPingbao")
    public R<String> delPingbao(@RequestBody ZsPingbao zsPingbao){
        boolean res=zsPingbaoService.removeById(zsPingbao);
        if(!res){
            throw new NotErrorException(EnumException.NO_DELETE);
        }
        return R.success("ok");
    };
    @PostMapping("/delGood")
    public R<String> delGood(@RequestBody ZsGood zsGood){
        boolean res=zsGoodService.removeById(zsGood);
        if(!res){
            throw new NotErrorException(EnumException.NO_DELETE);
        }
        return R.success("ok");
    };
    @RequestMapping(value={"/saveBack"},method= RequestMethod.POST)
    public R<String> saveBack(@RequestBody ZsBack zsBack){
        boolean res=zsBackService.updateById(zsBack);
        if(!res){
            throw new NotErrorException(EnumException.NO_UPDATE);
        }
        return R.success("ok");
    }
}
